import ast
import asyncio
from langchain_mcp_adapters.client import MultiServerMCPClient

async def main():
    client = MultiServerMCPClient(
        {#88.76.31.219
            "local_mcp": {
                "url": "http://127.0.0.1:9000/mymcp",  # MCP 服务  地址
                "transport": "sse",             # 使用 SSE 传输
                "headers": {}                   # 如果有 token，可在这里加 Authorization
            }
        }
    )

    tools = await client.get_tools()
    print("✅ 获取到的工具:", [t for t in tools])
    print("✅ 获取到的工具数量:", len(tools))
    for i, tool in enumerate(tools, 1):
        print(f"  {i}. 工具名称: {tool.name}")
        if hasattr(tool, 'description') and tool.description:
            print(f"     工具描述: {tool.description}")
        else:
            print(f"     工具描述: 暂无描述")
        print()

    for tool in tools:
        if tool.name == "greet":
            result = await tool.ainvoke({"name": "xiaolin"})
            print(f"✅ 调用结果: {result}")
        if tool.name == "getweather":
            result = await tool.ainvoke({"city": "太仓"})
            print(f"✅ 调用结果: {result}")
        if tool.name == "execute_code":
            result = await tool.ainvoke({"lang": "python","code": """
import os\nimport subprocess\nos.environ["ENVIRONMENT"] = "development"\nprint('hello world')\nprint('hello mcp')"""})
            print(f"✅ 调用结果: {result}")

if __name__ == "__main__":
    asyncio.run(main())
